package com.aaron.sqlSession;

import com.aaron.enums.SqlTypeEnum;
import com.aaron.pojo.Configuration;
import com.aaron.pojo.MappedStatement;

import java.lang.reflect.*;
import java.sql.SQLException;
import java.util.List;

public class DefaultSqlSession implements SqlSession {

    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public <E> List<E> selectList(String statementId, Object... params) throws SQLException, Exception {
        return selectList(configuration.getMappedStatementMap().get(statementId));
    }

    @Override
    public <T> T selectOne(String statementId, Object... params) throws Exception {
        return selectOne(configuration.getMappedStatementMap().get(statementId));
    }

    @Override
    public <E> List<E> selectList(MappedStatement mappedStatement, Object... params) throws Exception {

        //将要去完成对SimpleExecutor里的query方法调用
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        List<Object> list = simpleExecutor.query(configuration, mappedStatement, params);
        return (List<E>) list;
    }

    @Override
    public <T> T selectOne(MappedStatement mappedStatement, Object... params) throws Exception {

        List<Object> objects = selectList(mappedStatement, params);
        if (objects.size() == 1) {
            return (T) objects.get(0);
        } else {
            throw new RuntimeException("查询结果为空或者返回结果过多");
        }
    }

    public Integer insert(MappedStatement mappedStatement, Object... params) throws Exception {
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        //获取MappedStatement
        return simpleExecutor.insertOrUpdateOrDelete(configuration, mappedStatement, params);
    }

    private Integer update(MappedStatement mappedStatement, Object... params) throws Exception {
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        //获取MappedStatement
        return simpleExecutor.insertOrUpdateOrDelete(configuration, mappedStatement, params);
    }

    private Integer delete(MappedStatement mappedStatement, Object... params) throws Exception {
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        //获取MappedStatement
        return simpleExecutor.insertOrUpdateOrDelete(configuration, mappedStatement, params);
    }

    @Override
    public <T> T getMapper(Class<?> mapperClass) {
        // 使用JDK动态代理来为Dao接口生成代理对象，并返回
        Object proxyInstance = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{mapperClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                String methodName = method.getName();
                String className = method.getDeclaringClass().getName();
                String statementId = className + "." + methodName;
                //准备参数
                MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
                SqlTypeEnum sqlTypeEnum = mappedStatement.getSqlType();
                if (SqlTypeEnum.update.equals(sqlTypeEnum)) {
                    return update(mappedStatement, args);
                } else if (SqlTypeEnum.insert.equals(sqlTypeEnum)) {
                    return insert(mappedStatement, args);
                } else if (SqlTypeEnum.delete.equals(sqlTypeEnum)) {
                    return delete(mappedStatement, args);
                } else if (SqlTypeEnum.select.equals(sqlTypeEnum)) {
                    //获取被调用方法的返回值类型
                    Type genericReturnType = method.getGenericReturnType();
                    // 判断是否是泛型
                    System.out.println("是否是泛型: " + (genericReturnType instanceof ParameterizedType ? "true" : "false"));
                    if (genericReturnType instanceof ParameterizedType) {
                        return selectList(mappedStatement, args);
                    }
                    return selectOne(mappedStatement, args);
                }
                return null;
            }
        });
        return (T) proxyInstance;
    }


}
